//
//  ViewController.m
//  MyWeeX
//
//  Created by 仓鼠_Z on 2018/11/14.
//  Copyright © 2018 LeHe. All rights reserved.
//

#import "ViewController.h"
#import <WeexSDK/WeexSDK.h>
#import <WXDevtool/WXDevTool.h>
#import "MyNativeView.h"
@interface ViewController ()

@property (nonatomic, strong)  WXSDKInstance * instance;
@property (nonatomic, strong)  UIView *weexView;
@property (nonatomic, strong)  NSURL * url;

@property (weak, nonatomic) IBOutlet UIButton *myBtn;


@end

@implementation ViewController

-(void)clcik{
    NSLog(@"=======123");
    [self render ];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"----123---%s",__func__);
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn];
    btn.frame = CGRectMake(0, 88 , 60, 40);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"点我" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clcik ) forControlEvents:UIControlEventTouchUpInside];
    [self render];
 
}

- (void)render
{
    _instance = [[WXSDKInstance alloc] init];
    _instance.viewController = self;
    _instance.frame = CGRectMake(0,88, 414, 720 );
    
    __weak typeof(self) weakSelf = self;
    _instance.onCreate = ^(UIView *view) {
        [weakSelf.weexView removeFromSuperview];
        weakSelf.weexView = view;
        [weakSelf.view addSubview:weakSelf.weexView];
    };
    
    _instance.onFailed = ^(NSError *error) {
        //process failure
        NSLog(@"onFailed");
    };
    
    _instance.renderFinish = ^ (UIView *view) {
        NSLog(@"renderFinish");
    };
         NSString *str = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"js"];
        NSURL *url = [NSURL fileURLWithPath:str];
    [_instance renderWithURL:url];
//        NSString *randomURL = [NSString stringWithFormat:@"%@%@random=%d",url.absoluteString,url.query?@"&":@"?",arc4random()];
//    [_instance renderWithURL:url options:@{@"bundleUrl":[self.url absoluteString]} data:nil];
}
- (IBAction)click {

    NSLog(@"this is instance %@",self.instance);
    NSLog(@"this is weex View %@",self.weexView);
    
}

@end
